package cn.ellacat.game.guess;

import java.util.Set;

import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wjc133
 */
@Slf4j
public final class Game {
    private GameStatus status = GameStatus.READY;
    private final Set<GameListener> listeners = Sets.newHashSet();
    private IoController ioController;

    private static final Game INSTANCE = new Game();

    private Game() {
        // no-op
    }

    public static Game getInstance() {
        return INSTANCE;
    }

    public void addListener(final GameListener listener) {
        this.listeners.add(listener);
    }

    public void start() {
        if (this.status == GameStatus.READY || this.status == GameStatus.GAME_OVER) {
            this.status = GameStatus.GAMING;
            this.listeners.forEach(GameListener::onGameStart);
        }
    }

    public void end() {
        if (this.status == GameStatus.GAMING) {
            this.status = GameStatus.GAME_OVER;
            this.listeners.forEach(GameListener::onGameEnd);
        }
    }

    public void registryIoController(final IoController ioController) {
        this.ioController = ioController;
    }

    public IoController getIoController() {
        return this.ioController;
    }
}
